<?php defined('SYSPATH') or die('No direct script access.');

class Product_type_Controller extends Template_Controller 
{
	public function __construct()
	{
		parent::__construct();
	
		$this->template->menu = new View('menu');
	}
	
	public function index()
	{		
		$model = new Product_type_Model();
		
		$table = new View('product/type_lists');
		$table->caption = 'Product Type List';
		$table->datas = $model->find_all();;
		
		$this->template->content = $table->render();
	}
	
	public function create()
	{
		$form = new Forge(NULL, 'New Product Type');
		$form->input('name')->label(TRUE);
		$form->input('title_cn')->label('Chinese Title')->rules('required');
		$form->input('title_en')->label('English Title')->rules('required');
		$form->submit('Save');
 
		if($form->validate())
		{
			//Load model and set values
			$attribute = new Product_type_Model();
			$attribute->name = trim($form->name->value) == '' ? $form->title_cn->value : $form->name->value;
			$attribute->title_cn = $form->title_cn->value;
			$attribute->title_en = $form->title_en->value;
 
			//Save form into table
			if($attribute->save())
			{
				url::redirect('product_type');
			}
			else
			{
				echo 'no saving today';
			}
		}
		else
		{
		    $this->template->content = $form->render();
		}
	}
	
	public function edit($id)
	{
		$result = new Product_type_Model($id);

		$form = new Forge(NULL, 'Edit Product Type');
		$form->input('name')->label(TRUE)->value($result->name);
		$form->input('title_cn')->label('Chinese Title')->rules('required')->value($result->title_cn);
		$form->input('title_en')->label('English Title')->rules('required')->value($result->title_en);
		$form->submit('Save');
 
		if($form->validate())
		{
			$result->name = $form->name->value;
			$result->title_cn = $form->title_cn->value;
			$result->title_en = $form->title_en->value;
 
			//Save form into table
			if($result->save())
			{
				url::redirect('product_type');
			}
			else
			{
				echo 'no saving today';
			}
		}
		else
		{
		    $this->template->content = $form->render();
		}
	}
	
	public function delete($id)
	{
		$result = new Product_type_Model($id);
		$result->delete();
		
		url::redirect('product_type');
	}
	
	public function attribute($id)
	{
		$form = new Forge();
		$form->input('result');
		
		if($form->validate())
		{
			$arr = explode(',', $form->result->value);
			
			if(!empty($arr))
			{
				$model = new Product_type_attribute_Model();
				$model->where('product_type_id='.$id)->delete_all();
				
				for($i=0; $i<count($arr); $i++)
				{
					$model = new Product_type_attribute_Model();
					
					$model->product_type_id = $id;
					$model->attribute_id = $arr[$i];
					$model->sort = $i;
					
					$model->save();
				}
			}
			
			url::redirect('product_type');
		}
		else
		{
			$result = new Product_Attribute_Model();
			
			$view = new View('product/type_attribute');
			
			$view->title = '商品属性设置';
			$view->used_attribute = $result->getRelationedAttribute($id);
			$view->unused_attribute = $result->getRelationedAttribute($id, FALSE);
			
			$this->template->content = $view->render();
		}
	}
}